﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraNavBar;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            const int wmNclbuttondown = 161;
            const int wmSyscommand = 274;
            const int htcaption = 2;
            const int scMove = 61456;

            if ((m.Msg == wmSyscommand) && (m.WParam.ToInt32() == scMove))
            {
                return;
            }

            if ((m.Msg == wmNclbuttondown) && (m.WParam.ToInt32() == htcaption))
            {
                return;
            }

            base.WndProc(ref m);
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            try
            {
                rcMenu.Pages.Clear();
                FillMenu();
                //nbcMenu.group
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void FillMenu()
        {
            using (var context = new AccManagerEntities())
            {
                var lst = context.MenuMasters.ToList();
                foreach (var menu in lst.Where(x => x.ParentId == 0).OrderBy(x => x.MenuId))
                {
                    rcMenu.Pages.Add(new RibbonPage(menu.MenuName) { Tag = menu.MenuId });
                }

                foreach (RibbonPage page in rcMenu.Pages)
                {
                    if (page != null)
                    {
                        var subMenu = lst.Where(x => x.ParentId == Convert.ToInt64(page.Tag));
                        foreach (var menu in subMenu)
                        {
                            var index = page.Groups.Add(new RibbonPageGroup(menu.MenuName) { Tag = menu.MenuId });
                            foreach (var innerMenu in lst.Where(x => x.ParentId == menu.MenuId))
                            {
                                var bItem = page.Groups[index].ItemLinks.Add(new BarButtonItem()
                                {
                                    Caption = innerMenu.MenuName,
                                    Tag = innerMenu.MenuId
                                });
                                bItem.Item.ItemClick += Item_LinkClicked;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Group Is Null");
                    }
                }
                //foreach (var VARIABLE in COLLECTION)
                //{

                //}
            }
        }


        #region Commented FillMenu

        //private void FillMenu()
        //{
        //    using (var context = new AccManagerEntities())
        //    {
        //        var lst = context.MenuMasters.ToList();
        //        foreach (var menu in lst.Where(x => x.ParentId == 0).OrderBy(x => x.MenuId))
        //        {
        //            nbcMenu.Groups.Add(new NavBarGroup(menu.MenuName) { Tag = menu.MenuId, Expanded = true });
        //        }

        //        foreach (NavBarGroup grp in nbcMenu.Groups)
        //        {
        //            if (grp != null)
        //            {
        //                var subMenu = lst.Where(x => x.ParentId == Convert.ToInt64(grp.Tag));
        //                foreach (var menu in subMenu)
        //                {
        //                    var link = grp.AddItem();
        //                    link.Item.Caption = menu.MenuName;
        //                    link.Item.Tag = menu.MenuId;
        //                    link.Item.LinkClicked += Item_LinkClicked;
        //                }
        //            }
        //            else
        //            {
        //                Console.WriteLine("Group Is Null");
        //            }
        //        }
        //    }
        //}

        #endregion


        private void Item_LinkClicked(object sender, ItemClickEventArgs e)
        {
            //var item = sender as NavBarItem;
            if (e.Item != null)
            {
                ClsMenu oClsMenu = new ClsMenu();
                oClsMenu.OpenForm(Convert.ToInt64(e.Item.Tag), this);
            }

        }

        private void FrmMain_Resize(object sender, EventArgs e)
        {
            BackgroundImageLayout = ImageLayout.None;
            BackgroundImageLayout = ImageLayout.Stretch;
        }

        #region Interface Operations

        private void bItemNew_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (ActiveMdiChild != null)
                {
                    ((ICrud)ActiveMdiChild).ResetData();
                    ((ICrud)ActiveMdiChild).SetMode(ClsGlobalVarz.Mode.Add);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void bItemSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (ActiveMdiChild != null)
                {
                    if (((ICrud)ActiveMdiChild).OperationSave())
                        bItemNew_ItemClick(sender, e);

                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void bItemOpen_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (ActiveMdiChild != null)
                {
                    ((ICrud)ActiveMdiChild).OperationOpen();
                    ((ICrud)ActiveMdiChild).SetMode(ClsGlobalVarz.Mode.Open);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void bItemEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (ActiveMdiChild != null)
                {
                    ((ICrud)ActiveMdiChild).OperationEdit();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void bItemDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (ActiveMdiChild != null)
                {
                    if (((ICrud)ActiveMdiChild).OperationDelete())
                        bItemNew_ItemClick(sender, e);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        #endregion

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
            Application.ExitThread();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            Test otest = new Test();
            otest.Show();
        }

    }

}
